<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Storage orders</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TopicStorageOrders.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Storage orders<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>There are two different storage orders for matrices and two-dimensional arrays: column-major and row-major. This page explains these storage orders and how to specify which one should be used.</p>
<h1><a class="anchor" id="TopicStorageOrdersIntro"></a>
Column-major and row-major storage</h1>
<p>The entries of a matrix form a two-dimensional grid. However, when the matrix is stored in memory, the entries have to somehow be laid out linearly. There are two main ways to do this, by row and by column.</p>
<p>We say that a matrix is stored in <b>row-major</b> order if it is stored row by row. The entire first row is stored first, followed by the entire second row, and so on. Consider for example the matrix</p>
<p class="formulaDsp">
\[ A = \begin{bmatrix} 8 &amp; 2 &amp; 2 &amp; 9 \\ 9 &amp; 1 &amp; 4 &amp; 4 \\ 3 &amp; 5 &amp; 4 &amp; 5 \end{bmatrix}. \]
</p>
<p>If this matrix is stored in row-major order, then the entries are laid out in memory as follows:</p>
<div class="fragment"><div class="line">8 2 2 9 9 1 4 4 3 5 4 5 </div>
</div><!-- fragment --><p>On the other hand, a matrix is stored in <b>column-major</b> order if it is stored column by column, starting with the entire first column, followed by the entire second column, and so on. If the above matrix is stored in column-major order, it is laid out as follows:</p>
<div class="fragment"><div class="line">8 9 3 2 1 5 2 4 4 9 4 5 </div>
</div><!-- fragment --><p>This example is illustrated by the following <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> code. It uses the <a class="el" href="classEigen_1_1PlainObjectBase.html#ad12a492bcadea9b65ccd9bc8404c01f1">PlainObjectBase::data()</a> function, which returns a pointer to the memory location of the first entry of the matrix.</p>
<table class="example">
<tr>
<th>Example</th><th>Output </th></tr>
<tr>
<td><div class="fragment"><div class="line">Matrix&lt;int, 3, 4, ColMajor&gt; Acolmajor;</div>
<div class="line">Acolmajor &lt;&lt; 8, 2, 2, 9,</div>
<div class="line">             9, 1, 4, 4,</div>
<div class="line">             3, 5, 4, 5;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;The matrix A:&quot;</span> &lt;&lt; endl;</div>
<div class="line">cout &lt;&lt; Acolmajor &lt;&lt; endl &lt;&lt; endl; </div>
<div class="line"> </div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;In memory (column-major):&quot;</span> &lt;&lt; endl;</div>
<div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; Acolmajor.size(); i++)</div>
<div class="line">  cout &lt;&lt; *(Acolmajor.data() + i) &lt;&lt; <span class="stringliteral">&quot;  &quot;</span>;</div>
<div class="line">cout &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">Matrix&lt;int, 3, 4, RowMajor&gt; Arowmajor = Acolmajor;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;In memory (row-major):&quot;</span> &lt;&lt; endl;</div>
<div class="line"><span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; Arowmajor.size(); i++)</div>
<div class="line">  cout &lt;&lt; *(Arowmajor.data() + i) &lt;&lt; <span class="stringliteral">&quot;  &quot;</span>;</div>
<div class="line">cout &lt;&lt; endl;</div>
<div class="line"> </div>
</div><!-- fragment -->  </td><td><pre class="fragment">The matrix A:
8 2 2 9
9 1 4 4
3 5 4 5

In memory (column-major):
8  9  3  2  1  5  2  4  4  9  4  5  

In memory (row-major):
8  2  2  9  9  1  4  4  3  5  4  5  
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TopicStorageOrdersInEigen"></a>
Storage orders in Eigen</h1>
<p>The storage order of a matrix or a two-dimensional array can be set by specifying the <code>Options</code> template parameter for <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> or <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a>. As <a class="el" href="group__TutorialMatrixClass.html">The Matrix class</a> explains, the Matrix class template has six template parameters, of which three are compulsory (<code>Scalar</code>, <code>RowsAtCompileTime</code> and <code>ColsAtCompileTime</code>) and three are optional (<code>Options</code>, <code>MaxRowsAtCompileTime</code> and <code>MaxColsAtCompileTime</code>). If the <code>Options</code> parameter is set to <code>RowMajor</code>, then the matrix or array is stored in row-major order; if it is set to <code>ColMajor</code>, then it is stored in column-major order. This mechanism is used in the above <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> program to specify the storage order.</p>
<p>If the storage order is not specified, then <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> defaults to storing the entry in column-major. This is also the case if one of the convenience typedefs (<code>Matrix3f</code>, <code>ArrayXXd</code>, etc.) is used.</p>
<p>Matrices and arrays using one storage order can be assigned to matrices and arrays using the other storage order, as happens in the above program when <code>Arowmajor</code> is initialized using <code>Acolmajor</code>. <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> will reorder the entries automatically. More generally, row-major and column-major matrices can be mixed in an expression as we want.</p>
<h1><a class="anchor" id="TopicStorageOrdersWhich"></a>
Which storage order to choose?</h1>
<p>So, which storage order should you use in your program? There is no simple answer to this question; it depends on your application. Here are some points to keep in mind:</p>
<ul>
<li>Your users may expect you to use a specific storage order. Alternatively, you may use other libraries than <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, and these other libraries may expect a certain storage order. In these cases it may be easiest and fastest to use this storage order in your whole program.</li>
<li>Algorithms that traverse a matrix row by row will go faster when the matrix is stored in row-major order because of better data locality. Similarly, column-by-column traversal is faster for column-major matrices. It may be worthwhile to experiment a bit to find out what is faster for your particular application.</li>
<li>The default in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> is column-major. Naturally, most of the development and testing of the <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> library is thus done with column-major matrices. This means that, even though we aim to support column-major and row-major storage orders transparently, the <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> library may well work best with column-major matrices. </li>
</ul>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
